package org.tigr.htc.cmd;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.Serializable;
import java.util.Vector;

import org.apache.log4j.Logger;


/**
 * The <b>CommandSet</b> a set of Commands
 */
public class CommandSet extends CommandImp implements Serializable {

	private static final long serialVersionUID = -2088887236735033298L;

	/** The field <code>logger</code>           */
    static Logger logger = Logger.getLogger(CommandSet.class);

    /** The field <code>commands</code>           */
    protected Vector commands;

    /** The field <code>status</code>           */
    protected CommandSetStatus status;

    /**
     * The constructor <code>CommandSet</code> creates an instance of the class
     *
     *
     */
    public CommandSet() {
        type = "composite";
        commands = new Vector();
    }

    /**
     * The method <code>toString<code>
     *
     *
     * @return
     *
     */
    public String toString() {
        return CommandHelper.getInstance().marshal(this);
    }

    /**
     * Setter/Getters
     */

    /**
     * <code>getStatus</code> get CommandStatus Object
     *
     * @return a <code>CommandStatus</code> object
     */
    public CommandSetStatus getStatus() {
        return status;
    }

    /**
     * <code>setStatus</code> set the CommandStatus
     *
     * @param pStatus
     *            a <code>CommandStatus</code> value
     */
    public void setStatus(CommandSetStatus pStatus) {
        status = pStatus;

        status.setCommandSet(this);
    }


    /**
     * <code>getCommand</code> get the Command by specifying an index.
     * 
     * @param pIndex
     * @return
     * @throws java.lang.IndexOutOfBoundsException
     */
    public ICommand getCommand(int pIndex) throws java.lang.IndexOutOfBoundsException {

        logger.debug("In getCommand. Index is " + pIndex);

        if((pIndex < 0) || (pIndex > commands.size())) {
            throw new IndexOutOfBoundsException();
        }

        return(ICommand) commands.get(pIndex);
    }


    public synchronized void createCommand() { }
    public synchronized void updateCommand() { }



    /**
     * The method <code>getCommands<code>
     *
     * @return
     *
     */
    public Vector getCommands() {
        logger.debug("In getCommands.");

        return commands;
    }

    /**
     * The method <code>setCommand<code>
     * 
     * @param pIndex
     * @param pCommand
     *
     * @throws java.lang.IndexOutOfBoundsException
     *
     */
    public void setCommand(int pIndex, ICommand pCommand)
            throws java.lang.IndexOutOfBoundsException {

        logger.debug("In setCommand. Index is " + pIndex);

        if(pIndex < 0) {
            throw new IndexOutOfBoundsException();
        }

        commands.add(pIndex, pCommand);
    }

    /**
     * The method <code>addCommand<code>
     *
     * @param pCommand
     *
     * @throws java.lang.IndexOutOfBoundsException
     *
     */
    public void addCommand(ICommand pCommand) throws java.lang.IndexOutOfBoundsException {
        logger.debug("In addCommand");
        commands.add(pCommand);
    }

    /**
     * The method <code>getCommandsCount<code>
     *
     *
     * @return
     *
     */
    public int getCommandsCount() {
        return commands.size();
    }
}
